package model;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Query;
import javax.persistence.Transient;

import database.SingleEntityManagerFactory;

@Entity
public class SdkDynMapper extends Model {
	

	String nameDynMapper;
	String idDynMapper;
	String fgType;
	String cdXmlFile;

	
	private List<SdkDynMapperXsd> sdkDynMapperXsds;
	List<SdkDynRelation> listRelation;

	
	public SdkDynMapper(){
		listRelation = new ArrayList<SdkDynRelation>();
	}
	
	public SdkDynMapper(String name, String idMap, String fg, String cdXml, List<SdkDynRelation> listRelation) {
		this.nameDynMapper = name;
		this.idDynMapper = idMap;
		this.fgType = fg;
		this.cdXmlFile = cdXml;
		this.listRelation = listRelation;

		
	}
	
	@OneToMany(mappedBy = "mapping")
	public List<SdkDynRelation> getListRelation() {
		return listRelation;
	}


	public void setListRelation(List<SdkDynRelation> listRelation) {
		this.listRelation = listRelation;
	}


	@Id
	@GeneratedValue
	@Column(name="CDDYNMAPPER")
	@Override
	public int getId() {
		// TODO Auto-generated method stub
		return super.getId();
	}
	@Override
	public void setId(int id) {
		// TODO Auto-generated method stub
		super.setId(id);
	}

	public String getNameDynMapper() {
		return nameDynMapper;
	}

	public void setNameDynMapper(String name) {
		this.nameDynMapper = name;
	}

	public String getIdDynMapper() {
		return idDynMapper;
	}

	public void setIdDynMapper(String idMap) {
		this.idDynMapper = idMap;
	}

	public String getFgType() {
		return fgType;
	}

	public void setFgType(String fg) {
		this.fgType = fg;
	}

	public String getCdXmlFile() {
		return cdXmlFile;
	}

	public void setCdXmlFile(String cdXml) {
		this.cdXmlFile = cdXml;
	}
	
	@OneToMany (mappedBy="cdDynMapper")
	public List<SdkDynMapperXsd> getSdkDynMapperXsds() {
		return sdkDynMapperXsds;
	}

	public void setSdkDynMapperXsds(List<SdkDynMapperXsd> sdkDynMapperXsds) {
		this.sdkDynMapperXsds = sdkDynMapperXsds;
	}


	@Transient
	public SdkDynMapper getObject(String value)
	{
		List<String> indexList = new ArrayList<String>();
		EntityManager session = SingleEntityManagerFactory.getInstance().createEntityManager();
		Query query = session.createQuery("FROM SdkDynMapper where nameDynMapper = :a"); 
		query.setParameter("a", value); 
		List<SdkDynMapper> list = query.getResultList();
		if (!list.isEmpty())
		{
			return list.get(0);
		}
		else
		{
			return null;
		}
		
	}

	

	
	
}
	
	